home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / network / base / netkit-b.07a / netkit-b / NetKit-B-0.07A / talkd / table.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-16  |  6.1 KB  |  232 lines

  1. /*
  2.  * Copyright (c) 1983 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. /*
  35.  * From: @(#)table.c    5.7 (Berkeley) 2/26/91
  36.  */
  37. char table_rcsid[] = 
  38.   "$Id: table.c,v 1.3 1996/07/16 05:01:32 dholland Exp $";
  39.  
  40. /*
  41.  * Routines to handle insertion, deletion, etc on the table
  42.  * of requests kept by the daemon. Nothing fancy here, linear
  43.  * search on a double-linked list. A time is kept with each 
  44.  * entry so that overly old invitations can be eliminated.
  45.  *
  46.  * Consider this a mis-guided attempt at modularity
  47.  */
  48. #include <sys/param.h>
  49. #include <sys/time.h>
  50. #include <sys/socket.h>
  51. #include <protocols/talkd.h>
  52. #include <syslog.h>
  53. #include <unistd.h>
  54. #include <stdio.h>
  55. #include <stdlib.h>
  56. #include <string.h>
  57. #include "proto.h"
  58.  
  59. #define MAX_ID 16000    /* << 2^15 so I don't have sign troubles */
  60.  
  61. #define NIL ((TABLE_ENTRY *)0)
  62.  
  63. extern    int debug;
  64. static struct timeval tp;
  65.  
  66. typedef struct table_entry TABLE_ENTRY;
  67.  
  68. struct table_entry {
  69.     CTL_MSG request;
  70.     long    time;
  71.     TABLE_ENTRY *next;
  72.     TABLE_ENTRY *last;
  73. };
  74.  
  75. TABLE_ENTRY *table = NIL;
  76. static void deleteit(TABLE_ENTRY *ptr);
  77.  
  78. /*
  79.  * Look in the table for an invitation that matches the current
  80.  * request looking for an invitation
  81.  */
  82. CTL_MSG *
  83. find_match(CTL_MSG *request)
  84. {
  85.     TABLE_ENTRY *ptr;
  86.     time_t current_time;
  87.  
  88.     gettimeofday(&tp, NULL);
  89.     current_time = tp.tv_sec;
  90.     if (debug)
  91.         print_request("find_match", request);
  92.     for (ptr = table; ptr != NIL; ptr = ptr->next) {
  93.         if ((ptr->time - current_time) > MAX_LIFE) {
  94.             /* the entry is too old */
  95.             if (debug)
  96.                 print_request("deleting expired entry",
  97.                     &ptr->request);
  98.             deleteit(ptr);
  99.             continue;
  100.         }
  101.         if (debug)
  102.             print_request("", &ptr->request);
  103.         if (strcmp(request->l_name, ptr->request.r_name) == 0 &&
  104.             strcmp(request->r_name, ptr->request.l_name) == 0 &&
  105.              ptr->request.type == LEAVE_INVITE)
  106.             return (&ptr->request);
  107.     }
  108.     return NULL;
  109. }
  110.  
  111. /*
  112.  * Look for an identical request, as opposed to a complimentary
  113.  * one as find_match does 
  114.  */
  115. CTL_MSG *
  116. find_request(CTL_MSG *request)
  117. {
  118.     register TABLE_ENTRY *ptr;
  119.     time_t current_time;
  120.  
  121.     gettimeofday(&tp, NULL);
  122.     current_time = tp.tv_sec;
  123.     /*
  124.      * See if this is a repeated message, and check for
  125.      * out of date entries in the table while we are it.
  126.      */
  127.     if (debug)
  128.         print_request("find_request", request);
  129.     for (ptr = table; ptr != NIL; ptr = ptr->next) {
  130.         if ((ptr->time - current_time) > MAX_LIFE) {
  131.             /* the entry is too old */
  132.             if (debug)
  133.                 print_request("deleting expired entry",
  134.                     &ptr->request);
  135.             deleteit(ptr);
  136.             continue;
  137.         }
  138.         if (debug)
  139.             print_request("", &ptr->request);
  140.         if (strcmp(request->r_name, ptr->request.r_name) == 0 &&
  141.             strcmp(request->l_name, ptr->request.l_name) == 0 &&
  142.             request->type == ptr->request.type &&
  143.             request->pid == ptr->request.pid) {
  144.             /* update the time if we 'touch' it */
  145.             ptr->time = current_time;
  146.             return (&ptr->request);
  147.         }
  148.     }
  149.     return NULL;
  150. }
  151.  
  152. void
  153. insert_table(CTL_MSG *request, CTL_RESPONSE *response)
  154. {
  155.     register TABLE_ENTRY *ptr;
  156.     time_t current_time;
  157.  
  158.     gettimeofday(&tp, NULL);
  159.     current_time = tp.tv_sec;
  160.     request->id_num = new_id();
  161.     response->id_num = htonl(request->id_num);
  162.     /* insert a new entry into the top of the list */
  163.     ptr = (TABLE_ENTRY *)malloc(sizeof(TABLE_ENTRY));
  164.     if (ptr == NIL) {
  165.         syslog(LOG_ERR, "insert_table: Out of memory");
  166.         _exit(1);
  167.     }
  168.     ptr->time = current_time;
  169.     ptr->request = *request;
  170.     ptr->next = table;
  171.     if (ptr->next != NIL)
  172.         ptr->next->last = ptr;
  173.     ptr->last = NIL;
  174.     table = ptr;
  175. }
  176.  
  177. /*
  178.  * Generate a unique non-zero sequence number
  179.  */
  180. int
  181. new_id(void)
  182. {
  183.     static int current_id = 0;
  184.  
  185.     current_id = (current_id + 1) % MAX_ID;
  186.     /* 0 is reserved, helps to pick up bugs */
  187.     if (current_id == 0)
  188.         current_id = 1;
  189.     return current_id;
  190. }
  191.  
  192. /*
  193.  * Delete the invitation with id 'id_num'
  194.  */
  195. int
  196. delete_invite(int id_num)
  197. {
  198.     TABLE_ENTRY *ptr;
  199.  
  200.     ptr = table;
  201.     if (debug) syslog(LOG_DEBUG, "delete_invite(%d)", id_num);
  202.  
  203.     for (ptr = table; ptr != NIL; ptr = ptr->next) {
  204.         if (ptr->request.id_num == id_num)
  205.             break;
  206.         if (debug) print_request("", &ptr->request);
  207.     }
  208.     if (ptr != NIL) {
  209.         deleteit(ptr);
  210.         return SUCCESS;
  211.     }
  212.     return NOT_HERE;
  213. }
  214.  
  215. /*
  216.  * Classic delete from a double-linked list
  217.  */
  218. static void
  219. deleteit(TABLE_ENTRY *ptr)
  220. {
  221.  
  222.     if (debug)
  223.         print_request("deleteit", &ptr->request);
  224.     if (table == ptr)
  225.         table = ptr->next;
  226.     else if (ptr->last != NIL)
  227.         ptr->last->next = ptr->next;
  228.     if (ptr->next != NIL)
  229.         ptr->next->last = ptr->last;
  230.     free(ptr);
  231. }
  232.